package com.learning.spring.ioc.bean.lifecycle.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;

import java.beans.PropertyDescriptor;

/**
 * ClassName: LifeCycleInstantiationAwareBeanPostProcessor
 * Description: 容器级别接口 InstantiationAwareBeanPostProcessor （实例化 Instantiation 前后）
 * <p>
 * 1. InstantiationAwareBeanPostProcessor 该接口扩展了 Bean 后置处理器接口 BeanPostProcessor，一般继承该接口的实现类 InstantiationAwareBeanPostProcessorAdapter 即可。
 * 3. 注重处理时点：是在 Bean 的实例化（Instantiation）前后，实例化的过程是一个创建 Bean 的过程，即调用 Bean 的构造函数，单例的Bean放入单例池中
 * <p>
 * Date: 2018/8/3 13:24 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
public class LifeCycleInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {

    /**
     * 实例化前被调用
     *
     * @param beanClass
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        System.out.println("调用 InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation 方法 （实例化前被调用）");
        return super.postProcessBeforeInstantiation(beanClass, beanName);
    }

    /**
     * 实例化被后调用
     *
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        System.out.println("调用 InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation 方法 （实例化被后调用）");
        return super.postProcessAfterInstantiation(bean, beanName);
    }

    /**
     * 设置属性信息之前被调用
     *
     * @param pvs
     * @param pds
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
        PropertyValues propertyValues = super.postProcessPropertyValues(pvs, pds, bean, beanName);
        System.out.println("调用 InstantiationAwareBeanPostProcessor.postProcessPropertyValues 方法 （设置属性信息之前被调用）");
        return propertyValues;
    }
}
